package com.zhongkai.web.control.book;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDjCcdjxxLs;
import com.zhongkai.model.book.TDjCcywcl;
import com.zhongkai.model.config.User;
import com.zhongkai.service.book.CarInfoChangedService;
import com.zhongkai.service.book.CarRegisterService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.DateTool;

@Controller
public class CarRegisterControl {
	private Logger log=Logger.getLogger(this.getClass());
	
	
	private CarRegisterService carRegisterService;
	public CarRegisterService getCarRegisterService() {
		return carRegisterService;
	}
	@Resource
	public void setCarRegisterService(CarRegisterService carRegisterService) {
		this.carRegisterService = carRegisterService;
	}

	private CarInfoChangedService carInfoChangedService;
	public CarInfoChangedService getCarInfoChangedService() {
		return carInfoChangedService;
	}
	@Resource
	public void setCarInfoChangedService(CarInfoChangedService carInfoChangedService) {
		this.carInfoChangedService = carInfoChangedService;
	}
	
	
	
	@RequestMapping(value="/system/book/car_register_save.do")
	public void save(TDjCcdjxx tDjCcdjxx,HttpServletRequest request,HttpServletResponse response)throws Exception{
		PrintWriter out=response.getWriter();

		Date date = new Date();
		//01为"正常使用"
		tDjCcdjxx.setDjztDm("01");
		tDjCcdjxx.setCcpzh("粤T."+tDjCcdjxx.getCcpzh().toUpperCase());
		tDjCcdjxx.setSwcclxDm(carRegisterService.JjcclxDM2SwcclxDm(tDjCcdjxx.getJjcclxDm()).getSwcclxDm());
		tDjCcdjxx.setMsBj("N");
		
		User user =(User)request.getSession().getAttribute("user");
		tDjCcdjxx.setCzrDm(user.getUserIdentifier());
		tDjCcdjxx.setXgrDm(user.getUserIdentifier());
		
		tDjCcdjxx.setCzRq(date);
		tDjCcdjxx.setXgRq(date);


		//01为"登记"
		TDjCcywcl tDjCcywcl =carInfoChangedService.CreateNewTDjCcywcl("01", tDjCcdjxx.getCcdjh(), "", date, date, date, "", "", "", "", "", "", "", "", "", date, "", 0, date, date, date, date);
		carRegisterService.SaveCarRegister(tDjCcdjxx,tDjCcywcl);
		
		String[] nextCheck = request.getParameterValues("next");
		if(nextCheck == null){
			out.write(CheckWord.showMsg("保存车船登记信息成功！", "/system/book/car_register_form.jsp",request));
		}else{
			String czMc=URLEncoder.encode(URLEncoder.encode(tDjCcdjxx.getCzMc(),"UTF-8"),"UTF-8");
			String CzDz=URLEncoder.encode(URLEncoder.encode(tDjCcdjxx.getCzMc(),"UTF-8"),"UTF-8");
			
			out.write(CheckWord.showMsg("保存车船登记信息成功！", "/system/book/car_register_form.jsp" +
					"?czlbDm="+tDjCcdjxx.getCzlbDm()+"&nsrbm="+tDjCcdjxx.getNsrbm()+"&czMc="+czMc+"&czDz="+CzDz+"&czDh="+tDjCcdjxx.getCzDh()+""
					,request));
		}
	}

	@RequestMapping(value="/system/book/car_register_update.do")
	public void update(TDjCcdjxx tDjCcdjxx,TDjCcdjxxLs tDjCcdjxxLs,HttpServletRequest request,HttpServletResponse response)throws Exception{
		PrintWriter out=response.getWriter();
		
		if(!"01".equals(tDjCcdjxx.getDjztDm())){
			out.write(CheckWord.showMsg("该车船登记信息为非正常使用状态,不能保存！", "/system/book/car_register_update_form.jsp?ccdjh="+request.getParameter("ccdjh"),request));
			return;
		}
		
		Date date = new Date();
		TDjCcdjxx TDjCcdjxx_LS = carRegisterService.FindByCarNo(tDjCcdjxx.getCcdjh());
		BeanUtils.copyProperties(tDjCcdjxxLs,TDjCcdjxx_LS);
		
		tDjCcdjxx.setCcpzh("粤T."+tDjCcdjxx.getCcpzh().toUpperCase());
		tDjCcdjxx.setSwcclxDm(carRegisterService.JjcclxDM2SwcclxDm(tDjCcdjxx.getJjcclxDm()).getSwcclxDm());
		tDjCcdjxx.setCzrDm(TDjCcdjxx_LS.getCzrDm());
		tDjCcdjxx.setCzRq(TDjCcdjxx_LS.getCzRq());
		
		User user =(User)request.getSession().getAttribute("user");
		tDjCcdjxx.setXgrDm(user.getUserIdentifier());
		tDjCcdjxx.setXgRq(date);
		tDjCcdjxxLs.setXgrDm(user.getUserIdentifier());
		tDjCcdjxxLs.setXgRq(date);
		

		//09为"变更"
		TDjCcywcl tDjCcywcl =carInfoChangedService.CreateNewTDjCcywcl("09", TDjCcdjxx_LS.getCcdjh(), "", date, date, date, "", "", "", "", "", "", "", "", "", date, "", 0, date, date, date, date);			
		carRegisterService.UpdateCarRegister(tDjCcdjxx,tDjCcywcl,tDjCcdjxxLs);
		
		out.write(CheckWord.showMsg("保存车船登记信息成功！", "/system/search/unisearch.jsp?module=book/car_register_update_form.jsp",request));
	}

	@RequestMapping(value="/system/book/car_register_del.do")
	public void del(TDjCcdjxx tDjCcdjxx,TDjCcdjxxLs tDjCcdjxxLs,HttpServletRequest request,HttpServletResponse response)throws Exception{
		PrintWriter out=response.getWriter();
		
		if(!"01".equals(tDjCcdjxx.getDjztDm())){
			out.write(CheckWord.showMsg("该车船登记信息为非正常使用状态,不能删除！", "/system/book/car_register_update_form.jsp?ccdjh="+request.getParameter("ccdjh"),request));
			return;
		}

		Date date = new Date();
		TDjCcdjxx TDjCcdjxx_LS = carRegisterService.FindByCarNo(tDjCcdjxx.getCcdjh());
		BeanUtils.copyProperties(tDjCcdjxxLs,TDjCcdjxx_LS);
		
		User user =(User)request.getSession().getAttribute("user");
		TDjCcdjxx_LS.setXgrDm(user.getUserIdentifier());
		TDjCcdjxx_LS.setXgRq(date);
		tDjCcdjxxLs.setXgrDm(user.getUserIdentifier());
		tDjCcdjxxLs.setXgRq(date);

		//04为"已删除"
		TDjCcdjxx_LS.setDjztDm("04");

		//11为"管理员删除"
		TDjCcywcl tDjCcywcl =carInfoChangedService.CreateNewTDjCcywcl("11", TDjCcdjxx_LS.getCcdjh(), "", date, date, date, "", "", "", "", "", "", "", "", "", date, "", 0, date, date, date, date);			
		carRegisterService.UpdateCarRegister(TDjCcdjxx_LS,tDjCcywcl,tDjCcdjxxLs);
		
		out.write(CheckWord.showMsg("删除车船登记信息成功！", "/system/search/unisearch.jsp?module=book/car_register_update_form.jsp",request));
	}
	
	@RequestMapping(value="/system/book/car_register_gdt.do")
	public void generateDeclarationTime(String djRq,String gzRq,HttpServletRequest request,HttpServletResponse response)throws Exception{
		PrintWriter out=response.getWriter();
		try {
			if((djRq==null||"".equals(djRq)) || (gzRq==null||"".equals(gzRq))){
				return;
			}
			
			Calendar calendar=Calendar.getInstance();
			Date DateTemplate = DateTool.parse("2012-01-01", "yyyy-MM-dd");
			Date DatedjRq = DateTool.parse(djRq, "yyyy-MM-dd");
			Date DategzRq = DateTool.parse(gzRq, "yyyy-MM-dd");
			
			if (DategzRq.before(DateTemplate)){
				if(DatedjRq.before(DateTool.parse((DatedjRq.getYear()+1900)+"-"+(DatedjRq.getMonth()+1)+"-21","yyyy-MM-dd"))){
					calendar.setTime(DatedjRq);
					calendar.add(calendar.MONTH, 0);
					out.write(DateTool.formatDate(calendar.getTime()).replace("-", "").substring(0,6));
					
				}else {
					calendar.setTime(DatedjRq);
					calendar.add(calendar.MONTH, 1);
					out.write(DateTool.formatDate(calendar.getTime()).replace("-", "").substring(0,6));
				}
			}else{
				if(DategzRq.before(DateTool.parse((DategzRq.getYear()+1900)+"-"+(DategzRq.getMonth()+1)+"-21","yyyy-MM-dd"))){
					calendar.setTime(DategzRq);
					calendar.add(calendar.MONTH, 0);
					out.write(DateTool.formatDate(calendar.getTime()).replace("-", "").substring(0,6));
				}else {
					calendar.setTime(DategzRq);
					calendar.add(calendar.MONTH, 1);
					out.write(DateTool.formatDate(calendar.getTime()).replace("-", "").substring(0,6));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

}
